package com.example.dzchat;


import android.util.Log;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by 他的猫 on 2017/5/10.
 */

public class Okhttp {
    public static void postjson(final String url, String json,
                                final Objectcallback callback) {
        Log.e("测试", "url:"+url+",json:"+json);

        try {
            OkHttpUtils.postString()
                    .url(url)
                    .content(json)
                    .mediaType(MediaType.parse("application/json; charset=utf-8"))
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int i) {
//                            Log.e("DAZHI获取数据", "失败！" + e);
                            callback.onFalia(i, "网络异常,请检查网络！" + "失败！" + e);
                        }

                        @Override
                        public void onResponse(String responseString, int i) {
                            if (callback != null) {
//                                Log.e("DAZHI获取数据", url+"成功:" + responseString);

                                try {
                                    callback.onsuccess(responseString);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void post(String url, Map<String, String> paramters,
                            final Objectcallback callback) {
        Log.e("测试", "请求地址:" + url);
        Log.e("测试", "请求参数:" + paramters.toString());
        try {
            OkHttpUtils
                    .post()

                    .url(url)

                    .params(paramters)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                            callback.onFalia(id, e.toString());


                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                try {
                                    callback.onsuccess(responseString);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void get(String url, Map<String, String> paramters,
                           final Objectcallback callback) {

        try {
            OkHttpUtils
                    .get()
                    .url(url)
                    .params(paramters)
//                    .addHeader("Ticket", getimei)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            callback.onFalia(id, e.toString());
                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                try {
                                    callback.onsuccess(responseString);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public interface Objectcallback {
        void onsuccess(String st) throws Exception;

        void onFalia(int code, String errst);

    }

    public static void postFile2(String url, String file, String
            vin,final Objectcallback callback) {
        Log.e("TAG", "数据:" +url);
        try {
            OkHttpUtils
                    .post()
                    .url(url)
                    .addFile("file", file.substring(file.lastIndexOf("/") + 1, file.length()), new File(file))
                    .addParams("vin", vin)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            callback.onFalia(id, e.toString());


                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                try {
                                    callback.onsuccess(responseString);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
